﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using System.Reflection;

namespace Core
{
    public class Ioc
    {
        private static readonly IContainer _container;

        static Ioc()
        {
            _container = Build();
        }

        public static T Get<T>()
        {
            return _container.Resolve<T>();
        }

        #region
        static IContainer Build()
        {
            var builder = new ContainerBuilder();
            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
            .Where(t => t.Name.EndsWith("Notice"))
            .AsImplementedInterfaces();

            //如需加载实现的程序集，将dll拷贝到bin目录下即可，不用引用dll
            var IRepository = Assembly.Load("PZMCS.IRepository");
            var Repository = Assembly.Load("云课堂.Repository");

            //根据名称约定（数据访问层的接口和实现均以Repository结尾），实现数据访问接口和数据访问实现的依赖
            builder.RegisterAssemblyTypes(IRepository, Repository)
              .Where(t => t.Name.EndsWith("Repository"))
              .AsImplementedInterfaces();
            return builder.Build();
        }
        #endregion

    }
}
